import { type CrudConfig, type FormFieldConfig } from '@/components';
import urlConstants from '@/constants/urlConstants';
import type { ProColumns } from '@ant-design/pro-components';
import { Tag } from 'antd';

// 离线数据类型
export interface OfflineRecord {
  paramIndex?: number;
  tableEnglish: string;
  tableChinese: string;
  dimension: string;
  source: string;
  status: string;
  timeliness: string;
  updateUser?: string;
  updateTime?: string;
}

// 统一的页面配置函数 - 接收 translate 函数作为参数
export const getPageConfig = (translate: (prefix: string, key: string) => string) => {
  const t = (key: string) => translate('dataManage', key);
  const tCommon = (key: string) => translate('common', key);

  return {
    // CRUD配置
    crudConfig: {
      endpoints: {
        list: urlConstants.ADUIT_MANAGE.LIST,
        create: urlConstants.ADUIT_MANAGE.CREATE,
        update: urlConstants.ADUIT_MANAGE.EDIT,
        delete: urlConstants.ADUIT_MANAGE.DELETE,
        detail: urlConstants.ADUIT_MANAGE.LIST,
      },
    } as CrudConfig,

    // 搜索字段配置
    searchFields: [
      {
        name: 'tableEnglish',
        label: t('tableEnglish'),
        type: 'input' as const,
      },
      {
        name: 'tableChinese',
        label: t('tableChinese'),
        type: 'input' as const,
      },
    ],

    // 表格列配置
    columns: [
      {
        title: tCommon('paramIndex'),
        dataIndex: 'paramIndex',
        key: 'paramIndex',
        width: 80,
        fixed: 'left',
      },
      {
        title: t('tableEnglish'),
        dataIndex: 'tableEnglish',
        key: 'tableEnglish',
        width: 180,
        ellipsis: true,
      },
      {
        title: t('tableChinese'),
        dataIndex: 'tableChinese',
        key: 'tableChinese',
        width: 150,
        ellipsis: true,
      },
      {
        title: t('dimension'),
        dataIndex: 'dimension',
        key: 'dimension',
        width: 120,
      },
      {
        title: t('source'),
        dataIndex: 'source',
        key: 'source',
        width: 120,
      },
      {
        title: t('status'),
        dataIndex: 'status',
        key: 'status',
        width: 100,
        render: (text: string) => {
          const statusMap: Record<string, { color: string; text: string }> = {
            active: { color: 'green', text: tCommon('enabled') },
            inactive: { color: 'red', text: tCommon('disabled') },
          };
          const status = statusMap[text] || { color: 'default', text: text };
          return <Tag color={status.color}>{status.text}</Tag>;
        },
      },
      {
        title: t('timeliness'),
        dataIndex: 'timeliness',
        key: 'timeliness',
        width: 120,
      },
      {
        title: tCommon('updateUser'),
        dataIndex: 'updateUser',
        key: 'updateUser',
        width: 120,
      },
      {
        title: tCommon('updateTime'),
        dataIndex: 'updateTime',
        key: 'updateTime',
        width: 160,
        valueType: 'dateTime',
      },
    ] as ProColumns<OfflineRecord>[],

    // 表单字段配置
    formFields: [
      {
        name: 'tableEnglish',
        label: t('tableEnglish'),
        type: 'input' as const,
        rules: [{ required: true, message: t('tableEnglishRequired') }],
      },
      {
        name: 'tableChinese',
        label: t('tableChinese'),
        type: 'input' as const,
        rules: [{ required: true, message: t('tableChineseRequired') }],
      },
      {
        name: 'dimension',
        label: t('dimension'),
        type: 'input' as const,
        rules: [{ required: true, message: t('dimensionRequired') }],
      },
      {
        name: 'source',
        label: t('source'),
        type: 'input' as const,
      },
      {
        name: 'status',
        label: t('status'),
        type: 'select' as const,
        options: [
          { label: tCommon('enabled'), value: 'active' },
          { label: tCommon('disabled'), value: 'inactive' },
        ],
        rules: [{ required: true, message: t('statusRequired') }],
      },
      {
        name: 'timeliness',
        label: t('timeliness'),
        type: 'input' as const,
      },
    ] as FormFieldConfig[],

    // 字典枚举配置
    dictEnum: {},
  };
};
